Show link to validation on history page (for anons)
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @package MediaWiki
7 */
8
9 /**
10 * @todo document
11 * @package MediaWiki
12 */
13
14 include_once ( "SpecialValidate.php" ) ;
15
16 class PageHistory {
17 var $mArticle, $mTitle, $mSkin;
18 var $lastline, $lastdate;
19 var $linesonpage;
20 function PageHistory( $article ) {
21 $this->mArticle =& $article;
22 $this->mTitle =& $article->mTitle;
23 }
24
25 # This shares a lot of issues (and code) with Recent Changes
26
27 function history() {
28 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
29 $wgTitle, $wgUseValidation ;
30
31 # If page hasn't changed, client can cache this
32
33 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) ){
34 # Client cache fresh and headers sent, nothing more to do.
35 return;
36 }
37 $fname = 'PageHistory::history';
38 wfProfileIn( $fname );
39
40 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
41 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
42 $wgOut->setArticleFlag( false );
43 $wgOut->setArticleRelated( true );
44 $wgOut->setRobotpolicy( 'noindex,nofollow' );
45
46 $id = $this->mTitle->getArticleID();
47 if( $id == 0 ) {
48 $wgOut->addHTML( wfMsg( 'nohistory' ) );
49 wfProfileOut( $fname );
50 return;
51 }
52
53 $limit = $wgRequest->getInt('limit');
54 if (!$limit) $limit = 50;
55 $offset = $wgRequest->getText('offset');
56 if (!isset($offset) || !preg_match("/^[0-9]+$/", $offset)) $offset = 0;
57
58 /* Check one extra row to see whether we need to show 'next' and diff links */
59 $limitplus = $limit + 1;
60
61 $namespace = $this->mTitle->getNamespace();
62 $title = $this->mTitle->getText();
63 $uid = $wgUser->getID();
64 $db =& wfGetDB( DB_SLAVE );
65 if ($uid && $wgShowUpdatedMarker && $wgUser->getOption( 'showupdated' ))
66 $notificationtimestamp = $db->selectField( 'watchlist',
67 'wl_notificationtimestamp',
68 array( 'wl_namespace' => $namespace, 'wl_title' => $this->mTitle->getDBkey(), 'wl_user' => $uid ),
69 $fname );
70 else $notificationtimestamp = false;
71
72 $use_index = $db->useIndexClause( 'page_timestamp' );
73 $revision = $db->tableName( 'revision' );
74
75 $limits = $offsets = "";
76 $dir = 0;
77 if ($wgRequest->getText("dir") == "prev")
78 $dir = 1;
79
80 list($dirs, $oper) = array("DESC", "<");
81 if ($dir) {
82 list($dirs, $oper) = array("ASC", ">");
83 }
84
85 if ($offset)
86 $offsets .= " AND rev_timestamp $oper '$offset' ";
87 if ($limit)
88 $limits .= " LIMIT $limitplus ";
89
90 $sql = "SELECT rev_id,rev_user," .
91 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit ".
92 "FROM $revision $use_index " .
93 "WHERE rev_page=$id " .
94 $offsets .
95 "ORDER BY rev_timestamp $dirs " .
96 $limits;
97 $res = $db->query( $sql, $fname );
98
99 $revs = $db->numRows( $res );
100
101 if( $revs < $limitplus ) // the sql above tries to fetch one extra
102 $this->linesonpage = $revs;
103 else
104 $this->linesonpage = $revs - 1;
105
106 $atend = ($revs < $limitplus);
107
108 $this->mSkin = $wgUser->getSkin();
109
110 $pages = array();
111 $lowts = 0;
112 while ($line = $db->fetchObject($res)) {
113 $pages[] = $line;
114 }
115 if ($dir) $pages = array_reverse($pages);
116 if (count($pages) > 1)
117 $lowts = $pages[count($pages) - 2]->rev_timestamp;
118 else
119 $lowts = $pages[count($pages) - 1]->rev_timestamp;
120
121
122 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$offset}&limit={$limit}");
123 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$lowts}&limit={$limit}");
124 $urls = array();
125 foreach (array(20, 50, 100, 250, 500) as $num) {
126 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
127 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
128 }
129 $bits = implode($urls, ' | ');
130 $numbar = wfMsg("viewprevnext",
131 "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>",
132 "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>",
133 $bits);
134
135 $s = $numbar;
136 if($this->linesonpage > 0) {
137 $submitpart1 = '<input class="historysubmit" type="submit" accesskey="'.wfMsg('accesskey-compareselectedversions').
138 '" title="'.wfMsg('tooltip-compareselectedversions').'" value="'.wfMsg('compareselectedversions').'"';
139 $this->submitbuttonhtml1 = $submitpart1 . ' />';
140 $this->submitbuttonhtml2 = $submitpart1 . ' id="historysubmit" />';
141 }
142 $s .= $this->beginHistoryList();
143 $counter = 1;
144 foreach($pages as $line) {
145 $s .= $this->historyLine(
146 $line->rev_timestamp, $line->rev_user,
147 $line->rev_user_text, $namespace,
148 $title, $line->rev_id,
149 $line->rev_comment, ( $line->rev_minor_edit > 0 ),
150 $counter,
151 $notificationtimestamp,
152 ($counter == 1 && $offset == 0)
153 );
154 $counter++;
155 }
156 $s .= $this->endHistoryList( !$atend );
157 $s .= $numbar;
158
159 # Validation line
160 if ( isset ( $wgUseValidation ) && $wgUseValidation ) {
161 $s .= "<p>" . Validation::link2statistics ( $this->mArticle ) . "</p>" ;
162 }
163
164 $wgOut->addHTML( $s );
165 wfProfileOut( $fname );
166 }
167
168 function beginHistoryList() {
169 global $wgTitle;
170 $this->lastdate = $this->lastline = '';
171 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
172 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
173 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
174 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
175 $s .= !empty($this->submitbuttonhtml1) ? $this->submitbuttonhtml1."\n":'';
176 $s .= '<ul id="pagehistory">';
177 return $s;
178 }
179
180 function endHistoryList( $skip = false ) {
181 $last = wfMsg( 'last' );
182
183 $s = $skip ? '' : preg_replace( "/!OLDID![0-9]+!/", $last, $this->lastline );
184 $s .= '</ul>';
185 $s .= !empty($this->submitbuttonhtml2) ? $this->submitbuttonhtml2 : '';
186 $s .= '</form>';
187 return $s;
188 }
189
190 function historyLine( $ts, $u, $ut, $ns, $ttl, $oid, $c, $isminor, $counter = '', $notificationtimestamp = false, $latest = false ) {
191 global $wgLang, $wgContLang;
192
193 static $message;
194 if( !isset( $message ) ) {
195 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
196 $message[$msg] = wfMsg( $msg );
197 }
198 }
199
200 if ( $oid && $this->lastline ) {
201 $ret = preg_replace( "/!OLDID!([0-9]+)!/", $this->mSkin->makeKnownLinkObj(
202 $this->mTitle, $message['last'], "diff=\\1&oldid={$oid}",'' ,'' ,' tabindex="'.$counter.'"' ), $this->lastline );
203 } else {
204 $ret = '';
205 }
206 $dt = $wgLang->timeanddate( $ts, true );
207
208 if ( $oid ) {
209 $q = 'oldid='.$oid;
210 } else {
211 $q = '';
212 }
213 $link = $this->mSkin->makeKnownLinkObj( $this->mTitle, $dt, $q );
214
215 if ( 0 == $u ) {
216 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
217 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
218 htmlspecialchars( $ut ), 'target=' . urlencode( $ut ) );
219 } else {
220 $userPage =& Title::makeTitle( NS_USER, $ut );
221 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $ut ) );
222 }
223
224 $s = '<li>';
225 if ( $oid && !$latest ) {
226 $curlink = $this->mSkin->makeKnownLinkObj( $this->mTitle, $message['cur'],
227 'diff=0&oldid='.$oid );
228 } else {
229 $curlink = $message['cur'];
230 }
231 $arbitrary = '';
232 if( $this->linesonpage > 1) {
233 # XXX: move title texts to javascript
234 $checkmark = '';
235 if ( !$oid || $latest ) {
236 $arbitrary = '<input type="radio" style="visibility:hidden" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'" />';
237 $checkmark = ' checked="checked"';
238 } else {
239 if( $counter == 2 ) $checkmark = ' checked="checked"';
240 $arbitrary = '<input type="radio" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'"'.$checkmark.' />';
241 $checkmark = '';
242 }
243 $arbitrary .= '<input type="radio" name="diff" value="'.$oid.'" title="'.$message['selectnewerversionfordiff'].'"'.$checkmark.' />';
244 }
245 $s .= "({$curlink}) (!OLDID!{$oid}!) $arbitrary {$link} <span class='user'>{$ul}</span>";
246 $s .= $isminor ? ' <span class="minor">'.$message['minoreditletter'].'</span>': '' ;
247
248
249 $s .= $this->mSkin->commentBlock( $c, $this->mTitle );
250 if ($notificationtimestamp && ($ts >= $notificationtimestamp)) {
251 $s .= wfMsg( 'updatedmarker' );
252 }
253 $s .= '</li>';
254
255 $this->lastline = $s;
256 return $ret;
257 }
258
259 }
260
261 ?>